package com.example.parsing.producer;

import com.alibaba.fastjson.JSONObject;
import com.example.parsing.unit.NoGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class TxtProducer {
	
	private static final Logger logger = LoggerFactory.getLogger(TxtProducer.class);
	
	public static Map<String,String> idMap = new HashMap<>();
	
	/**
	 * esn 后台生成，保证唯一性
	 * deviceName 唯一性，采用设备型号
	 * type 设备的类型
	 */
	
	public void register(String esn, String deviceName, String type, KafkaTemplate<String,String>kafkaTemplate){
		try{
			JSONObject registerJson = new JSONObject();
			JSONObject json = new JSONObject();
			json.put("serialNumber", esn);
			json.put("modelName", type);
			json.put("status", 2);
			json.put("deviceName", deviceName);
			String deviceTitle = deviceName+"的实验数据";
			json.put("deviceTitle",deviceTitle);
			String uuid = NoGenerator.getUUID();
			registerJson.put("id",uuid);
			registerJson.put("requestTime", String.valueOf(NoGenerator.getDate()));
			registerJson.put("request",json);
			logger.info("准备发送消息，消息的内容是="+registerJson.toJSONString());
			kafkaTemplate.send("sys." + type + ".subdevice.register.request",type,registerJson.toJSONString());
			idMap.put(uuid,deviceName);
			logger.info("发送消息的map储存的key="+uuid+"，value="+deviceName);
			//637f495866224e94972e37aa1f53d5ca
		}catch (Exception e){
			logger.error("txt文本的Json的注册失败，错误类型是="+e);
		}
	}
}
